$(function () {
    let songid = getQueryString('songid');

    //用户问题
    addUser();

    $("#submit").click(function () {
        addEstimate(songid);
    });

    playSongMv(songid);
    /**
     * 加载评论
     */
    addEstimateBySong(songid);
});

function addUser() {
    $.get(path + "/user/user-info", {}, function (result) {
        if (result.code === 200) {
            let user = result.data;
            $("#estimate_user_name").html(user.userName);
            $("#estimate_user_pic").attr("src", user.headImgUrl);
            $("#player_estimate_reply").show();
        } else {
            alert(result.msg);
        }
    });
}

function playSongMv(songid) {
    $.get(path + "/song/song/get-param", {"id": songid,"type":0}, function (result) {
        if (result.code === 200) {
            let data = result.data;
            $("#song_name_span").html(data.songName);
            $("#song_pic_img").attr("src", data.headImg);
            // $("#video_mv").attr("src", '../mv/' + data.mv);
            // $("#video_mv").attr("autoplay", 'autoplay');

            let video='<video id="video_mv" src="'+data.mv+'" autoplay loop controls></video>';
            $("#video").html(video);
        } else {
            alert(result.msg);
        }
    });
}

/**
 * 加载评论
 */
function addEstimateBySong(songid) {
    $.get(path + "/user/user-song-estimate/list-use", {
        "songId": songid,
        "pageNum": 1,
        "pageSize": 10
    }, function (result) {
        if (result.code === 200) {
            $("#player_estimate").empty();
            var list = result.list;
            $(list).each(function () {
                appendEstimate(this.songName, this.userHeadImg, this.userName, this.estimateInfo);
            });
        } else {
            alert(result.msg);
        }
    });
}

function appendEstimate(songName, userHeadImg, userName, estimateInfo) {
    var div = '<div class="r_r">' +
        '<div class="master">评论歌曲：' + songName + '</div>' +
        '<div class="r_r1"><div class="r_r11"><img src="' + userHeadImg + '"/></div><div class="r_r12">' + userName + '</div></div>' +
        '<div class="r_r2"><div class="r_r21">&nbsp;&nbsp; 评论内容：</div><div class="r_r22">' + estimateInfo + '</div></div>' +
        '</div>';
    $("#player_estimate").append(div);
}

/**
 * 发表评论
 */
function addEstimate(songid) {
    let estimateInfo = $("#textarea").val();
    if (estimateInfo === "") {
        alert("评论不能为空！");
        return;
    }
    $.post(path + "/user/user-song-estimate", {"songId": songid, "estimateInfo": estimateInfo}, function (result) {
        if (result.code === 200) {
            let data = result.data;
            appendEstimate(data.songName, data.userHeadImg, data.userName, data.estimateInfo);
            $("#textarea").val("");
        } else {
            alert(result.msg);
        }
    });
}

